package com.earl.eatbao.web;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.earl.eatbao.commons.vo.ResultMessage;
import com.earl.eatbao.domain.eatbao.BusinessApply;
import com.earl.eatbao.service.BusinessApplyService;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;

/**
 * @author Administrator
 * @version 1.0
 * @created 22-十一月-2016 22:34:46
 */
@RestController
@RequestMapping("/businessApply")
public class BusinessApplyController extends BaseController{

	private final Logger logger = LoggerFactory.getLogger(BusinessApplyController.class);
	
    @Autowired
    private BusinessApplyService businessApplyService;
    
	/**
	 * 商家申请认证审核失败
	 * @param applyId
	 */
	@ApiOperation(value = "商家申请认证审核失败", notes = "商家申请认证审核失败", httpMethod = "POST", response = String.class)
	@RequestMapping(value="/fail/{applyId}",  method = RequestMethod.POST)
	public ResponseEntity<ResultMessage> failOnShopApply(
			@ApiParam(name = "applyId", required = true, value = "申请的ID号")
			@PathVariable Long applyId){
        logger.info("<====== 进入failOnShopApply:方法,{[applyId] : " + applyId + " } ====");
        // desc: 封装操作结果
		ResultMessage result = new ResultMessage();
		businessApplyService.failOnBusinessApply(applyId);
		result.setServiceResult(true);
		result.setMessageCode(200l);
		result.setResultInfo("请求操作成功");

		logger.info("==== 退出failOnShopApply方法 返回值类型:{ ResponseEntity<ResultMessage>: } =======>");
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}

	/**
	 * 商家申请认证审核成功
	 * @return
	 */
	@ApiOperation(value = "商家申请认证审核成功", notes = "商家申请认证审核成功", httpMethod = "POST", response = String.class)
	@RequestMapping(value="/success/{applyId}",  method = RequestMethod.POST)
	public ResponseEntity<ResultMessage> successOnShopApply(
			@ApiParam(name = "applyId", required = true, value = "申请的ID")
			@PathVariable Long applyId){

		logger.info("<====== 进入successOnShopApply:方法,{[applyId] :" + applyId + " } ====");

		// desc: 封装操作结果
		ResultMessage result = new ResultMessage();
		businessApplyService.successOnBusinessApply(applyId);
		result.setServiceResult(true);
		result.setMessageCode(200l);
		result.setResultInfo("请求操作成功");

		logger.info("==== 退出successOnShopApply方法 返回值类型:{ ResponseEntity<ResultMessage>: } =======>");
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}

	/**
	 * 用户认证为商家
	 * @return
	 */
    @ApiOperation(value = "用户认证为商家", notes = "用户认证为商家", httpMethod = "POST", response = String.class)
    @RequestMapping(value="/add",  method = RequestMethod.POST)
	public ResponseEntity<ResultMessage> beShopperApply(
	        @ApiParam(name = "desc", required = true, value = "说明介绍")
	        @RequestParam(name = "desc", required = true) String desc ,
            @ApiParam(name = "userId", required = true, value = "用户ID")
            @RequestParam(name = "userId", required = true) Long userId,
            @ApiParam(value="身份证背景图片") MultipartFile backImg,
            @ApiParam(value="身份证正面图片") MultipartFile fontImg){

        logger.info("<====== 进入beShopperApply:方法,{[businessApply] : } ====");
        // desc: 封装操作结果
		ResultMessage result = new ResultMessage();
        result.setServiceResult(businessApplyService.beBusinessApply(desc, userId, backImg, fontImg));
		if(result.getServiceResult()) {
			result.setResultInfo("请求操作成功");
		} else {
			result.setResultInfo("请求操作成功");
		}
		result.setMessageCode(200l);

		logger.info("==== 退出beShopperApply方法 返回值类型:{ ResponseEntity<ResultMessage>: } =======>");
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}

    /**
     * 查询所用待审核的商家
     * @return
     */
	@ApiOperation(value = "查询所用待审核的商家", notes = "查询所用待审核的申请", httpMethod = "GET", response = String.class)
	@RequestMapping(value="/waitApply",  method = RequestMethod.GET)
	public ResponseEntity<ResultMessage> findAllWaitApplyBusiness() {

        logger.info("<====== 进入findAllWaitApplyBusiness:方法,{[] : } ====");

        // desc: 封装操作结果
        ResultMessage result = new ResultMessage();
        List<BusinessApply> businessApplies = businessApplyService.findAllWaitApplyBusiness();
        result.getResultParm().put("applyList", businessApplies);
        result.setServiceResult(true);
        result.setMessageCode(200l);
        result.setResultInfo("请求操作成功");

        logger.info("==== 退出findAllWaitApplyBusiness方法 返回值类型:{ ResponseEntity<ResultMessage>: } =======>");
        return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
    }

    /**
     * 查找所用通过审核的商家
     * @return
     */
    @ApiOperation(value = "查找所用通过审核的商家", notes = "查找所用通过审核的申请", httpMethod = "GET", response = String.class)
    @RequestMapping(value="/passApply",  method = RequestMethod.GET)
    public ResponseEntity<ResultMessage> findAllPassApplyBusiness() {
        logger.info("<====== 进入findAllPassApplyBusiness:方法,{[] : } ====");

        // desc: 封装操作结果
        ResultMessage result = new ResultMessage();
        result.getResultParm().put("applyList", businessApplyService.findAllPassApplyBusiness());
        result.setMessageCode(200l);
        result.setResultInfo("请求操作成功");

        logger.info("==== 退出findAllPassApplyBusiness方法 返回值类型:{ ResponseEntity<ResultMessage>: } =======>");
        return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
    }

    /**
     * 查找所用申请不通过的用户
     * @return
     */
    @ApiOperation(value = "查找所用申请不通过的用户", notes = "查找所用申请不通过的申请", httpMethod = "GET", response = String.class)
    @RequestMapping(value="/failApply",  method = RequestMethod.GET)
    public ResponseEntity<ResultMessage> findAllFailApplyBusiness() {

        logger.info("<====== 进入findAllFailApplyBusiness:方法,{[] : } ====");

        // desc: 封装操作结果
        ResultMessage result = new ResultMessage();
        result.getResultParm().put("applyList", businessApplyService.findAllFailApplyBusiness());
        result.setMessageCode(200l);
        result.setResultInfo("请求操作成功");

        logger.info("==== 退出findAllFailApplyBusiness方法 返回值类型:{ ResponseEntity<ResultMessage>: } =======>");
        return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
    }
}